{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24020efb-1f76-40cc-9ca0-4d419a94163c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from linearmodels.panel import PanelOLS\n",
    "import statsmodels.api as sm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3d48000-8f71-4dd1-ae12-ba548ae15155",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load Excel data\n",
    "df = pd.read_excel(\"df.xlsx\")\n",
    "\n",
    "# Set panel structure\n",
    "df = df.set_index(['district', 'year'])\n",
    "\n",
    "# Add entity (district) and time (year) fixed effects\n",
    "# Create dummies for year (like i.year in Stata)\n",
    "year_dummies = pd.get_dummies(df.index.get_level_values('year'), prefix='year', drop_first=True)\n",
    "df = df.join(year_dummies)\n",
    "\n",
    "# Define independent variables\n",
    "X_base = [\n",
    "    'birth_rate', 'foreign_ratio', 'population_growth',\n",
    "    'urban_ratio', 'conservative_ratio'\n",
    "] + list(year_dummies.columns)\n",
    "\n",
    "# Add constant manually for PanelOLS\n",
    "df['const'] = 1\n",
    "\n",
    "# Model 1: immigration_mentions ~ X + year FE + district FE\n",
    "model_immigration = PanelOLS(\n",
    "    df['immigration_mentions'],\n",
    "    df[X_base + ['const']],\n",
    "    entity_effects=True\n",
    ")\n",
    "results_immigration = model_immigration.fit(cov_type='clustered', cluster_entity=True)\n",
    "\n",
    "# Model 2: sentiment_gap ~ X + year FE + district FE\n",
    "model_sentiment = PanelOLS(\n",
    "    df['sentiment_gap'],\n",
    "    df[X_base + ['const']],\n",
    "    entity_effects=True\n",
    ")\n",
    "results_sentiment = model_sentiment.fit(cov_type='clustered', cluster_entity=True)\n",
    "\n",
    "# Print results\n",
    "print(results_immigration.summary)\n",
    "print(results_sentiment.summary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0845442f-3e1f-43a5-8f05-73acef3b1295",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecfe8e15-7155-47d0-8d2d-b61070342d70",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
